package br.mikhas.lab.lang;

import br.mikhas.lab.lang.tables.FieldTable;
import br.mikhas.lab.lang.tables.StringTable;

public class Field {

	int id;
	int name;
	Class cls;
	Class type;
	int offset = 0;
	private final StringTable strings;

	public Field(StringTable strings, FieldTable fields, String name,
			Class type, Class cls) {
		this.strings = strings;
		this.name = strings.get(name);
		this.cls = cls;
		this.type = type;
		this.id = fields.get(this);
		cls.addField(this);
	}

	void setOffset(int offset) {
		this.offset = offset;
	}

	String getName() {
		return this.strings.get(this.name);
	}

	int getId() {
		return this.id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((cls == null) ? 0 : cls.hashCode());
		result = prime * result + name;
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Field other = (Field) obj;
		if (cls == null) {
			if (other.cls != null)
				return false;
		} else if (!cls.equals(other.cls))
			return false;
		if (name != other.name)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Field[" + this.getId() + ":" + this.cls.getName() + "."
				+ this.getName() + "]";
	}

}
